﻿/* File javascript phai duoc dat ten la module.js */
  
 /* Doan code nay co tac dung ket noi file javascript va file view*/
 // ===============================================Code Hảo viết===================================
M.local_myplugin = {
	Y: null,

    ajaxurl: M.cfg.wwwroot+'/local/myplugin/ajax.php',

    menu_program: null,

    menu_major: null,
	
	cbview_pro: null,
	
	cbblock:null,
	
	init: function(Y) {
        this.Y = Y;
        this.menu_program = Y.one('#id_cbprogram');
        this.menu_major = Y.one('#id_cbmajor');
		this.cbview_pro = Y.one('#id_cbvi_pro');
		this.cbview_block = Y.one('#id_cbvi_block');
		
		if(this.cbview_pro!=null)
		{
			this.cbview_pro.on('change', function(e) {
            M.local_myplugin.get_blocks_of_program();
			M.local_myplugin.get_majors_of_program();
			});
			this.cbview_pro.on('keypress', function(e) {
            M.local_myplugin.get_blocks_of_program();
			M.local_myplugin.get_majors_of_program();
			});
		}
		if(this.menu_program!=null)
		{
			this.menu_program.on('change', function(e) {
            M.local_myplugin.get_majors();
			});
			
			this.menu_program.on('keypress', function(e) {
				M.local_myplugin.get_majors();
			});

		}
		if(this.cbview_block!=null)
		{
			this.cbview_block.on('change', function(e) {
				M.local_myplugin.get_majors_of_program();
			});
			
			this.cbview_block.on('keypress', function(e) {
				M.local_myplugin.get_majors_of_program();
			});

		}
    },
	get_blocks_of_program: function() {
        
		Y = this.Y;		
		var program_id = this.cbview_pro.get('value');
		
		Y.io(this.ajaxurl, {
            data: 'timtheo=block&program='+program_id,
            method: 'get',
            on: {
                success: function(id, o) {
                    response = Y.JSON.parse('{"settings":'+o.responseText+'}');
					var list = Y.Node.create('<select id="id_cbvi_block" style="min-width:200px;" />');
					list.appendChild(Y.Node.create('<option selected="selected" value="">Any Block of Knowledge</option>'));
                    for (s in response.settings) {
                        option = Y.Node.create('<option value="'+response.settings[s].bok_id+'">'+response.settings[s].bok_name+'</option>')
                        list.appendChild(option);
                    }
                    Y.one('#id_cbvi_block').replace(list);
                    Y.one('#id_cbvi_block').on('change', function(e) {
                        M.local_myplugin.get_majors_of_program();
                    });
                    
                }
            }
        });
    },
	
	
	get_majors_of_program: function() {
        
		Y = this.Y;		
		var program_id = this.cbview_pro.get('value');
		var block_id = document.getElementById("id_cbvi_block").value;

		Y.io(this.ajaxurl, {
            data: 'timtheo=majorofprogram&program='+program_id+','+block_id,
            method: 'get',
            on: {
                success: function(id, o) {
				
                    response = Y.JSON.parse('{"settings":'+o.responseText+'}');
                    var list = Y.Node.create('<select id="id_cbvi_major" style="min-width:200px;" />');
					list.appendChild(Y.Node.create('<option selected="selected" value="">Any Major</option>'));
                    for (s in response.settings) {
                        option = Y.Node.create('<option value="'+response.settings[s].major_id+'">'+response.settings[s].major_name+'</option>')
                        list.appendChild(option);
                    }
                    Y.one('#id_cbvi_major').replace(list);
                }
            }
        });
    },
	
	
    get_majors: function() {
        
		Y = this.Y;		
		var program_id = this.menu_program.get('value');
		Y.io(this.ajaxurl, {
            data: 'timtheo=major&program='+program_id,
            method: 'get',
            on: {
                success: function(id, o) {
				
                    response = Y.JSON.parse('{"settings":'+o.responseText+'}');
                    var list = Y.Node.create('<select id="id_cbmajor" class="block_form" size="10" style="min-width:200px;" name="cbmajor[]" multiple="multiple"  />');
                    for (s in response.settings) {
                        option = Y.Node.create('<option value="'+response.settings[s].major_id+'">'+response.settings[s].major_name+'</option>')
                        list.appendChild(option);
                    }
                    Y.one('#id_cbmajor').replace(list);
                    Y.one('#id_cbmajor').on('change', function(e) {
                        M.local_myplugin.get_setting();
                    });
                }
            }
        });
    },
	get_all_blocks: function() {
		Y = this.Y;		
		Y.io(this.ajaxurl, {
            data: 'timtheo=allblock&program=123',
            method: 'get',
            on: {
                success: function(id, o) {
                    response = Y.JSON.parse('{"settings":'+o.responseText+'}');
                    var list = Y.Node.create('<select id="id_cbblock" style="min-width:200px;"/>');
					list.appendChild(Y.Node.create('<option selected="selected" value="">Any</option>'));
                    for (s in response.settings) {
                        option = Y.Node.create('<option value="'+response.settings[s].bok_id+'">'+response.settings[s].bok_name+'</option>')
                        list.appendChild(option);
                    }
                    Y.one('#id_cbblock').replace(list);
                    
                }
            }
        });
    },
	
	get_all_los: function() {
		Y = this.Y;	
		var program_id =$("#id_cblo_program").val();
		
		Y.io(this.ajaxurl, {
            data: 'timtheo=alllos&program='+program_id,
            method: 'get',
            on: {
                success: function(id, o) {
					$("select[name=cbparentlevel]").html('');
                    $("select[name=cbparentlevel]").append(o.responseText);
					
					  var options = {}; // an empty object
						options.selectablecategories= "true";
						options.mode= "compact";
						var id=$("select[name=cbparentlevel]").attr("id");
					   M.form.init_smartselect(Y,id, options);
                }
            }
        });
    },
	
};

 

 //lay danh sach cac mon hoc theo program, block, major (trang dùng : view_program.php)
function view_subject_list()
{
	var program, block, major;
	program =$("#id_cbvi_pro").val();
	block =$("#id_cbvi_block").val();
	major =$("#id_cbvi_major").val();
	$.ajax({
		  type: 'GET',
		  url: "getsubject.php?param="+program+","+block+","+major,
		  data:'',
		  dataType: 'html',
		  success: function(msg){
			  $('#divtest').html(msg);
		  },
	});
	
} 
function load_block_desc()
{
	var pro_major =$("select[name=cbprogram]").val();
	var block =$("#id_cbblock").val();
	$.ajax({
		  type: 'GET',
		  url: "ajax.php?timtheo=desc&program="+pro_major+"+"+block,
		  data:'',
		  dataType: 'html',
		  success: function(msg){
			  frames[0].document.body.innerHTML=msg;
		  },
	});
	
}
function openColorBox(duongdan) {         
    $.fn.colorbox({
			width:"60%", 
			href:duongdan,
			title:"Edit Subject",
			height:"80%", 
			iframe:true, 
			onCleanup:function(){
				view_subject_list();
			}
		});
}
//ham nay duoc dung de mo colorbox voi duong dan truyen vao
//sau khi colorbox duoc dong lai thi goi hàm loadcombobox() với tham so la duong dan;
function openColorBox_update_list(duongdan) {         
    $.fn.colorbox({
			width:"60%", 
			href:duongdan,
			title:"Edit Subject",
			height:"80%", 
			iframe:true, 
			onClosed:function(){
				loadcombobox(duongdan);
			}
		});
}
//ham nay kiem tra tuy theo đường dẫn mà load lại combobox 
function loadcombobox(duongdan)
{
	if(duongdan=='quick_add_major.php')
	{
		M.local_myplugin.get_majors();	
	}
	else if(duongdan=='quick_add_bok.php')
	{
		M.local_myplugin.get_all_blocks();	
	}
	
	
}

//Chọn các môn học từ danh mục môn học và gán vào danh sách các môn được chọn
//truyen them so la ten combobox danh sach và combobox được chọn
function choosecb(cblist, cbchon)
{
	var selectedArray = new Array();
	var SelectedValues = document.getElementById(cblist.id);
	var chosenValues = document.getElementById(cbchon.id);
	var sophantudachon = chosenValues.options.length;
	var i;
	var count = 0;
	for (i=0; i<SelectedValues.options.length; i++) {
		if (SelectedValues.options[i].selected) {	
			var text=SelectedValues.options[i].text;
			var value=SelectedValues.options[i].value;
			chosenValues.options[sophantudachon] = new Option(text, value);
			sophantudachon++;
			count++;
		}
	}
	
}
//truyen them so la ten combobox
function removecb(cbchon) {
  var removedsubject = document.getElementById(cbchon.id);
  var i;
  for (i = removedsubject.length - 1; i>=0; i--) {
    if (removedsubject.options[i].selected) {
      removedsubject.remove(i);
    }
  }
}

function set_selected(cbchon)
{
	var cbchon = document.getElementById(cbchon.id);
	var i;
	for (i = cbchon.length - 1; i>=0; i--) 
	{
		cbchon.options[i].selected=true;
	}
}


function load_major_table()
{
	var program=$('#id_cboProgram').val();
	$.ajax({
		  type: 'GET',
		  url: "ajax.php?timtheo=allmajor&program="+program,
		  data:'',
		  dataType: 'html',
		  success: function(msg){
			$('#major_tb').show();
			  $('#major_tb').html(msg);
		  },
	});
	
}
//dung cho trang add_program.php
function load_program_table()
{
	$.ajax({
		  type: 'GET',
		  url: "ajax.php?timtheo=allprogram&program=123",
		  data:'',
		  dataType: 'html',
		  success: function(msg){
			  $('#programList').html(msg);
		  },
	});
	clear_form_elements("#mform1");
}
function load_bok_table()
{
	$.ajax({
		  type: 'GET',
		  url: "ajax.php?timtheo=allblocks&program=123",
		  data:'',
		  dataType: 'html',
		  success: function(msg){
			  $('#major_tb').html(msg);
		  },
	});
	$('#id_txtten').val('');
	$('#id_txtlevel').val('');
	$('#id_txttype').val('');
	frames[0].document.body.innerHTML='';
} 

function preview_lo(filepath)
{
	var hinh='<div id="mess" class="loading">Loading...<div>';
	$('#lodiv').html(hinh);
	$.ajax({
		  type: 'GET',
		  url: "../ajax.php?timtheo=previewlo&program="+filepath,
		  data:'',
		  dataType: 'html',
		  success: function(msg){

			  $('#lodiv').html(msg);
		  },
		  error: function()
		  {
			$('#lodiv').html('<div id="mess" class="error">Loading Error</div>');
		  }
	});
	
}
function import_lo(filepath)
{
	var hinh='<div id="mess" class="loading">Importing...<div>';
	$('#lodiv').html(hinh);
	var programid=$('#id_cbprogram').val();
	$.ajax({
		  type: 'GET',
		  url: "../ajax.php?timtheo=importlo&program="+filepath+'^'+programid,
		  data:'',
		  dataType: 'html',
		  success: function(msg){

			  $('#lodiv').html(msg);
		  },
		  error: function()
		  {
			$('#lodiv').html('<div id="mess" class="error">Loading Error</div>');
		  }
	});
	
}
function import_subject(filepath)
{
	alert('test');
	var hinh='<div id="mess" class="loading">Importing...<div>';
	$('#lodiv').html(hinh);
	var programid=$('#id_cbprogram').val();
	$.ajax({
		  type: 'GET',
		  url: "../ajax.php?timtheo=importsubject&program="+filepath+'^'+programid,
		  data:'',
		  dataType: 'html',
		  success: function(msg){

			  $('#lodiv').html(msg);
		  },
		  error: function()
		  {
			$('#lodiv').html('<div id="mess" class="error">Loading Error</div>');
		  }
	});
	
}

$(document).ready(function()
{
	initToolTips();
	$("#red").treeview({
		animated: "fast",
		collapsed: true,
		control: "#treecontrol"
	});
	
	
	$('#red li').each(function() {
		$(this).simpletip({
		   showEffect: 'slide',
		   hideEffect: 'slide',
		   position: 'top',
		   content : $(this).attr('title')
		});
	});

});
// ============================ tạo tooltip Position====================================

	var toolTipSTYLE;

	function initToolTips(){
	
	  if(document.getElementById){
			  toolTipSTYLE = document.getElementById("toolTipLayer").style;
	  }
	  if(is_ie || is_nav6up)
	  {
		toolTipSTYLE.visibility = "visible";
		toolTipSTYLE.display = "none";
		document.onmousemove = moveToMousePos;
	  }
	}
	function moveToMousePos(e)
	{
		var offsetX = 0;
		var offsetY = -100;
		var opacity = 100;
	  if(!is_ie){
		x = e.pageX;
		y = e.pageY;
	  }else{
		x = event.x + document.body.scrollLeft;
		y = event.y + document.body.scrollTop;
	  }
	  toolTipSTYLE.left = x + offsetX+'px';
	  toolTipSTYLE.top = y + offsetY+'px';
	  return true;
	}


	function toolTip(msg, fg, bg)
	{
	  if(toolTip.arguments.length < 1) // if no arguments are passed then hide the tootip
	  {
		if(is_nav4)
			toolTipSTYLE.visibility = "hidden";
		else
			toolTipSTYLE.display = "none";
	  }
	  else // show
	  {
		if(!fg) fg = "#777777";
		if(!bg) bg = "#ffffe5";
		var content ='<div style="background:'+fg+'; padding:7px; max-width:400px; min-width:400px; z-index:9999;"><div style="background:'+bg+'; padding:7px; z-index:9999;"><font color="' + fg + '">'+msg+'</font></div></div>';
		
	   if(is_nav4)
		{
		  toolTipSTYLE.document.write(content);
		  toolTipSTYLE.document.close();
		  toolTipSTYLE.visibility = "visible";
		}

		else if(is_ie || is_nav6up)
		{
		  document.getElementById("toolTipLayer").innerHTML = content;
		  toolTipSTYLE.display='block'
		}
	  }
	}

// ============================ ket thuc tạo tooltip Position====================================

	//============================= chỉnh hàm export ================================
function export_lo()
{
	var programid=$('#id_cblo_program').val();
	$('#lodiv').html('<div class="loading">Writing Learning Outcomes To Excel File...</div>');
	$.ajax({
		  type: 'GET',
		  url: "../ajax.php?timtheo=exportlo&program="+programid,
		  data:'',
		  dataType: 'html',
		  success: function(msg){
		  $('#lodiv').html('<div id="mess" class="success"></div>');
			  //success_mess("");
			  $('#mess').html('Learning Outcomes Are Exported Successfully!'+msg)
			  //$('#mess').append(msg);
		  },
		  error: function()
		  {
			$('#lodiv').html('<div id="mess" class="error">Exporting Error</div>');
		  }
	});
	$('#mess').delay(7000).fadeOut('slow');
}
//dung cho trang add_block
var myValue;
function setValue(value)
{
    myValue = value;
}

function getValue()
{
	
    return window.myValue; // yup, it's "test"
}
function get_all_subject_for_add_block()
{
	var program=$("select[name=program]").val();
	//alert("Program: "+program);
	if(program!=getValue())
	{
		$.ajax({
			  type: 'GET',
			  url: "ajax.php?timtheo=allsubjects_for_addblock&program="+program,
			  data:'',
			  dataType: 'html',
			  success: function(msg){
				  $('#monhoc').html(msg);
				  setValue(program+'');
			  },
			  error: function()
			  {
				$('#monhoc').html('<div id="mess" class="error">Loading Subject Error</div>');
			  }
		});
	}

	setValue(program+"");
}

//Tuấn sửa
function get_all_subject_for_block_modification()
{
	var program=$("#program").attr('programid');
	var block=$("#block").attr('blockid');
	
	if(program!=getValue())
	{
		$.ajax({
			  type: 'GET',
			  url: "ajax.php?timtheo=allsubjects_for_modifyblock&program="+program+"&block="+block,
			  data:'',
			  dataType: 'html',
			  success: function(msg){
				  $('#monhoc').html(msg);
				  setValue(program+'');
			  },
			  error: function()
			  {
				$('#monhoc').html('<div id="mess" class="error">Loading Subject Error</div>');
			  }
		});
	}

	setValue(program+"");
}
//dung cho trang add_subject
function get_all_subject_for_add_subject()
{
	var program_id=$('#id_cbprogram').val();
	if(program_id!='all')
	{
		$.ajax({
			  type: 'GET',
			  url: "ajax.php?timtheo=allsubjects_for_addsubject&program="+program_id,
			  data:'',
			  dataType: 'html',
			  success: function(msg){
				$('#mess').show();
				$('#listmon').show();
				$('#subject_tb').show();
				  $('#subject_tb').html(msg);
				  success_mess('Subjects are loaded successfully!');
			  },
			  error: function()
			  {
				$('#monhoc').html('<div id="mess" class="error">Loading Subject Error</div>');
			  }
		});
	}
	else	
	{
		$('#listmon').hide();
		$('#subject_tb').hide();
		$('#mess').hide();
	}
}
// ===============================================Code Tuấn viết===================================

//Lay các chuyên ngành tương ứng với chương trình đào tạo được chọn
function showMajor() {
    var programid=$('#id_cbprogram_view').val();
	$.ajax({
		  type: 'GET',
		  url: "ajax.php?timtheo=major_for_view_curr&program="+programid,
		  data:'',
		  dataType: 'html',
		  success: function(msg){

			  $('#id_cbmajor_view').html('');
			  $('#id_cbmajor_view').html(msg);
		  },
		  error: function()
		  {
			alert('loi');
		  }
	});
}

/*Lấy cấu trúc chương trình đào tạo, bao gồm
 *Các khối kiến thức chung 
 *Các chuyên ngành
 *Các khối kiến thức của từng chuyên ngành
 */
 function showCurriculumStructure(){
 
	var programid =$('#cbo_academic_program').val();
	$.ajax({
		  type: 'GET',
		  url: "ajax.php?program="+programid+"&timtheo=get_block_by_program",
		  data:'',
		  dataType: 'html',
		  success: function(msg){
			  $('#cboblock').html(msg);
		  },
		  error: function()
		  {
			alert('loi');
		  }
	});
	
 }
 
 /*
 * Lấy các chuyên ngành của 1 chương trình đào tạo
 */ 
 function showMajor(program) {
    
    if (program == "") {
        document.getElementById("major").innerHTML = "";
       // return;
    }
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("major").innerHTML = xmlhttp.responseText;
			
        }
    }
    xmlhttp.open("GET", "get_major.php?program=" + program, true);
    xmlhttp.send();
 }
 
//hiển thị chương trình đào tạo được chọn
function showCurriculum(program) {
    var select = document.getElementById("MajorList");
    
    var major ="";  //Lưu danh sách các chuyên ngành
    var i = 0;
    for ( var i = 1; i<select.options.length; i++ ) {//Tìm chuyên ngành đầu tiên được chọn trong combobox
       if( select.options[i].selected) {
            major = select.options[i].value;
            break;
        }
    };
    i++;
    for (;i<select.options.length; i++) { //Lưu danh sách chuyên ngành theo format: mã chuyên ngành_1+mã chuyên ngành_2+.... 
      if( select.options[i].selected){ 
            major = major + "*" + select.options[i].value;  
      }
    };
    
    if (program == "") {
        document.getElementById("Curriculum").innerHTML = "";
       // return;
    }
    if (window.XMLHttpRequest) {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {
        // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("Curriculum").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "getcurriculum.php?q=" + program + "&p=" + major, true);
    xmlhttp.send();
}


function clear_form_elements(ele) {
    $(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
			case 'iframe':
                this.contents().find("body").val('');
        }
    });
}
function success_mess(mess)
{
	$("#mess").html(mess);
	$("#lodiv").show();
	$('#mess').delay(7000).fadeOut('slow');
}

function get_subject_for_view_curr(majors)
{
	var programid =$('#id_cbprogram_view').val();

	$.ajax({
		  type: 'GET',
		  url: "getcurriculum.php?program="+programid+"&majors="+majors,
		  data:'',
		  dataType: 'html',
		  success: function(msg){
			  $('#Curriculum').html(msg);
		  },
		  error: function()
		  {
			alert('loi');
		  }
	});
	clear_form_elements('#mform1');
}
function get_subject_for_export_curr(majors)
{
	$('#lodiv').show();
	$('#lodiv').html('<div id="mess" class="loading">Exporting...<div>');
	var programid =$('#id_cbprogram_view').val();
	$.ajax({
		  type: 'GET',
		  url: "exportcurriculum.php?program="+programid+"&majors="+majors,
		  data:'',
		  dataType: 'html',
		  success: function(msg){
			$('#lodiv').html('<div id="mess" class="success"><div>');
			  success_mess("Curriculum Is Exported Successfully!");
		  },
		  error: function()
		  {
			alert('loi');
		  }
	});
	clear_form_elements('#mform1');
}
//dung cho trang add_bok
function add_row_to_table_subject(id, subjectid, subjectname, credit, theory, practice)
{
	if ($('#'+id).is(':checked'))
	{
		//them dong của bảng selected subject
		$('#tb_selected_subject').append('<tr id="'+id+'"><td>'+subjectid+'</td><td>'+subjectname+'</td><td align="center">'+credit+'</td><td align="center">'+theory+'</td><td align="center"><input type="checkbox" value="'+id+'" name="selectedsubject[]" checked="checked" style="display: none;"/>'+practice+'</td></tr>');
		$('#selected_subject').show();
	}
	else
	{
		//xoa dong của bảng selected subject
		$('#'+id).remove();
		//dem so dong cua bang
		var rowCount = $('#tb_selected_subject tr').length;

		if(rowCount==1)
		{
			$('#selected_subject').hide();
			$('#tb_selected_subject').hide();
		}
	}
}

//========================================= EDIT INFORMATION AREA ==============================
function edit_subject(subject_id)
 {
	 $.fn.colorbox({
			width:"60%", 
			href:'quick_edit_subject.php?sid'+subject_id,
			title:"Edit Subject",
			height:"80%", 
			iframe:true
		});
 }
 function edit_program(program_id)
 {
	 $.fn.colorbox({
			width:"60%", 
			href:'quick_edit_program.php?id='+program_id,
			title:"Edit Subject",
			height:"80%", 
			iframe:true, 
			onClosed:function(){
				load_program_table();
			}
		});
 }
